package com.neo.web.controller.pub;

import com.neo.common.annotation.Log;
import com.neo.common.constant.UserConstants;
import com.neo.common.core.controller.BaseController;
import com.neo.common.core.domain.AjaxResult;
import com.neo.common.core.page.TableDataInfo;
import com.neo.common.enums.BusinessType;
import com.neo.common.utils.StringUtils;
import com.neo.common.utils.poi.ExcelUtil;
import com.neo.framework.shiro.service.SysPasswordService;
import com.neo.framework.util.ShiroUtils;
import com.neo.pub.domain.SMSUser;
import com.neo.pub.service.ISMSUserService;
import com.neo.system.domain.SysRole;
import com.neo.system.domain.SysUser;
import com.neo.system.domain.SysUserRole;
import com.neo.system.service.ISysPostService;
import com.neo.system.service.ISysRoleService;
import com.neo.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息
 * 
 * @author tr
 */
@Controller
@RequestMapping("/pub/sms")
public class SmsUserController extends BaseController
{
    private String prefix = "pub/sms";

    @Autowired
    private ISMSUserService ismsUserService;

    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SMSUser smsUser)
    {
        if(StringUtils.isNotEmpty(smsUser.getUserId()) || StringUtils.isNotEmpty(smsUser.getUserName())
         || StringUtils.isNotEmpty(smsUser.getTelphone())){
            smsUser.setEnable("");
        } else {
            smsUser.setEnable("1");
        }
        startPage();
        List<SMSUser> list = ismsUserService.selectSMSUserList(smsUser);
        return getDataTable(list);
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SMSUser smsUser)
    {
        return toAjax(ismsUserService.insertUser(smsUser));
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") int id, ModelMap mmap)
    {
        mmap.put("user", ismsUserService.selectSMSUserById(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SMSUser user)
    {
        user.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(ismsUserService.updateUser(user));
    }

    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(ismsUserService.deleteUserByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SMSUser user)
    {
        return toAjax(ismsUserService.changeStatus(user));
    }
}